﻿@model KPIS.GERP.WEB.GIMS.Controllers.AbsenceAdminFilter

@{
    Layout = null;

    string[] countAbleStatus = new[] {"U", "A"};
    
    var context = new GERPContext(true);
    
    var employees = context.Employees
        .Include(o => o.MasNamePrefix)
        .Include(o => o.MasEmployeeType)
        .Include(o => o.Position)
        .Include(o => o.MasEmployeeStatus)
        .Where(o => o.RecordStatus != "D")
        .OrderBy(o => o.EmpTypeId)
        .ThenBy(o => o.Position.PosNo)
        .ToList();
        
    var absencesQuery = context.PsAbsences.Include(o=>o.AbsenceType).Where(o => o.RecordStatus != "D").ToList();
    var recordStatusIncluded = Model.GetRecordStatusFiltered().ToArray();
    List<PsAbsence> absences = absencesQuery
        .Where(o => recordStatusIncluded.Contains(o.RecordStatus))
        .ToList();
    
    
    var tasks = context.Tasks
        //.Include(o=>o.CurrentTaskHistory.ApproverPosition)
        .Include(o=>o.ApproveProcess.ApproveProcessGroup)
        .Where(o => o.ApproveProcess != null &&
            o.ApproveProcess.AppPrcGrpId == 1 &&
            o.RecordStatus != "D")
            .ToList();

    var poses = context.Positions.ToList(); //o => o.PosId == s.ApproverPosId);
    foreach (var task in tasks)
    {
        if (task != null && task.Simulates != null && task.Simulates.Any())
        {
            foreach (var sim1 in task.Simulates)
            {
                sim1.ApproverPosition = poses.FirstOrDefault(o => o.PosId == sim1.ApproverPosId);
            }
        }
    }
}
 
<div id="report-loading" class="loading">
    <img src="~/Content/images/Photo/white_283x212_loading.gif" width="100px" height="50px" /><br /><strong>กำลังโหลด...</strong>
</div>
<table border="1" class="Table1 ">
    
    <!-- Title -->
    <tr style="font-weight: bold; background:rgb(227, 227, 227)">
        <td style="width: 200px;" rowspan="3">
            เจ้าหน้าที่
        </td>
        <td colspan="@DateTime.DaysInMonth(Model.Year, Model.Month)">
            @(new DateTime(Model.Year - 543, Model.Month, 1).ToString("MMMM yyyy"))
        </td>
        <td style="width:80px" rowspan="3">ยอดรวมที่มีผลแล้ว (วัน)</td>
    </tr>
    
    <!-- Day -->
    <tr style="font-weight: bold; background:rgb(227, 227, 227)">
        @for (int i = 1; i <= DateTime.DaysInMonth(Model.Year, Model.Month); i++)
        {
            var dayOfWeek = new DateTime(Model.Year - 543, Model.Month, i).DayOfWeek;
            <td @Html.Raw(dayOfWeek == DayOfWeek.Saturday || dayOfWeek == DayOfWeek.Sunday ? " class='styleNonWorkingDay'" : "")>
                @i
            </td>
        }
    </tr>
    
    <!-- Day of week -->
    <tr style="font-weight: bold; background:rgb(227, 227, 227)">
        @for (int i = 1; i <= DateTime.DaysInMonth(Model.Year, Model.Month); i++)
        {
            var dayOfWeekTh = new DateTime(Model.Year - 543, Model.Month, i).ToString("ddd");
            var dayOfWeek = new DateTime(Model.Year - 543, Model.Month, i).DayOfWeek;
            <td @Html.Raw(dayOfWeek == DayOfWeek.Saturday || dayOfWeek == DayOfWeek.Sunday ? " class='styleNonWorkingDay'" : "")>
                @dayOfWeekTh
            </td>
        }
    </tr>
    
    @{
    
        string jobSiteNameRunning = "";
        string empTypeNameRunning = "";
        string jobSiteIdRunning = "";
        string empTypeIdRunning = ""; 
    }

    @foreach (var e in employees 
        .OrderBy(o => o.EmpTypeId)
        .ThenBy(o => o.JobSite.JobSiteId)
        // .ThenBy(o => (o.JobSite.Code ?? "").PadLeft(8, '0'))
        .ThenBy(o => o.Position.PosNo))
    {
        if (e.MasEmployeeType.EmpTypeId + "" != empTypeIdRunning)
        {
            empTypeIdRunning = e.MasEmployeeType.EmpTypeId + "";
            empTypeNameRunning = e.MasEmployeeType.Name;
            <tr>
                <td colspan="100%" style="text-align: left; background-color:rgb(211, 230, 234)">
                    &nbsp;&nbsp;<span style="font-style: italic; font-size: 16px;font-weight: bold; color:rgb(83, 64, 16)">
                                    ประเภทบุคคลากร: @empTypeNameRunning
                                </span>
                </td>
            </tr>
        }
    
        if (e.JobSite.JobSiteId +"" != jobSiteIdRunning)
        {
            jobSiteIdRunning = e.JobSite.JobSiteId + "";
            jobSiteNameRunning = e.JobSite.Name;
            <tr>
                <td colspan="100%" style="text-align: left; background-color: rgb(231, 243, 245)">
                    &nbsp;&nbsp;<span style=" font-size: 13px; color:rgb(76, 83, 91); "> 
                                    @((e.JobSite.Code ?? "").PadLeft(10, ' ')) - @jobSiteNameRunning 
                                    @(e.JobSite.JobSiteTypeId == 1 ? " (" +  e.JobSite.MasJobSiteType.Name + ")" : "")</span>
                </td>
            </tr>
        }

        float totalOfEmp = 0f;
        var absenceOfEmp = absences.Where(o => o.EmpId == e.EmpId).ToList();
        <tr>
            <td style="text-align: left; ">
                <span title="@e.Position.PosNo - @e.Position.Name [@e.MasEmployeeType.Name]">
                    @e.Position.PosNo: @e.FullName 
                    @if (e.MasEmployeeStatus.Active == "0")
                    {
                        <span style="color:red">
                            [@e.MasEmployeeStatus.Name]
                        </span>
                    }
                </span>
            </td>
            @for (int i = 1; i <= DateTime.DaysInMonth(Model.Year, Model.Month); i++)
            {
                var day = new DateTime(Model.Year - 543, Model.Month, i);
                var dayOfWeek = day.DayOfWeek;

                <td @Html.Raw(dayOfWeek == DayOfWeek.Saturday || dayOfWeek == DayOfWeek.Sunday ? " class='styleNonWorkingDay' " : "")>
                    
                    @{
                        foreach (var a in absenceOfEmp)
                        {
                            if (!a.StartDate.HasValue || !a.EndDate.HasValue)
                            {
                                <span style="color: red">พบการลา @a.Code ไม่มีวันเริ่มลา</span>
                                continue;
                            }
                            if (day > a.EndDate.Value || day < a.StartDate.Value)
                            {
                                // the absence is not for this day
                                continue;
                            } 
            
                            string recordStatusName = KPIS.GERP.WEB.GIMS.Controllers.AbsenceAdminController.GetThaiText(a.RecordStatus);

                            float fractalOfDay;
                            if (day == a.StartDate.Value && a.StartTime == "A"){fractalOfDay = 0.5f;}
                            else if (day == a.EndDate.Value && a.EndTime == "M"){fractalOfDay = 0.5f;}
                            else { fractalOfDay = 1; }

                            if (countAbleStatus.Contains(a.RecordStatus))
                            {
                                // count valid and completed absence only use, approve status
                                totalOfEmp += fractalOfDay; 
                            }
            
                            var t = tasks.FirstOrDefault(o => o.RefId == a.AbsenceId);
                            if (t != null && t.CurrentTaskHistory != null && !t.Completed)
                            {
                                recordStatusName += " กำลังรอ " +
                                                    t.CurrentTaskHistory.ApproverPosition.PosNo + " - " +
                                                    t.CurrentTaskHistory.ApproverPosition.Name + " พิจารณา";
                            }

                            string title = a.AbsenceId + ": " + @a.RequestTitle +
                                           "วันลา " + @a.StartDate.Value.ToString("dd/MM/yyyy") + " " +
                                           (a.StartTime == "M" ? "ตั้งแต่ช่วงเช้า" : "ตั้งแต่ช่วงบ่าย") +
                                           "ถึง " + @a.EndDate.Value.ToString("dd/MM/yyyy") + " " +
                                           (a.EndTime == "M" ? "ถึงช่วงเช้า" : "ถึงช่วงบ่าย") +
                                           "สถานะ " + @recordStatusName;

                            <a name="linkShowTask" href="@Url.Action("Details", new { id=a.AbsenceId })" 
                               target="_blank" title='@title' class="nondecore @a.RecordStatus ">
                                @Html.Raw(countAbleStatus.Contains(a.RecordStatus) ?
                                    "<span style='text-decoration:underline'>" + fractalOfDay + "</span>" : "" + fractalOfDay)
                            </a>
                        }
                    }
                </td>
            }
            <td class="celltotal">
                @(totalOfEmp <= 0f ? "-" : totalOfEmp.ToString("#,##0.#"))
            </td>
        </tr>
    }
</table>
รวม @employees.Count คน (ปฎิบัติงาน @employees.Count(o=>o.MasEmployeeStatus.Active == "1") คน)